#include<iostream>
using namespace std;
#include<vector>
#include<queue>
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        if (root == nullptr)
            return {};
        queue<TreeNode*> que;
        que.push(root);
        vector<int> ans;
        while (!que.empty()) {
            int maxmun = INT_MIN;
            int size = que.size();
            while (size--) {
                TreeNode* now = que.front();
                que.pop();
                maxmun = max(maxmun, now->val);
                if (now->left)
                    que.push(now->left);
                if (now->right)
                    que.push(now->right);
            }
            ans.push_back(maxmun);
        }
        return ans;
    }
};